<?php
/**
 * 额度管理
 */
namespace App\Http\Models;

use App\Http\Util\quota\aop\request\AlipayFundAuthOperationCancelRequest;
use App\Http\Util\quota\aop\request\AlipayFundAuthOperationDetailQueryRequest;
use Illuminate\Database\Eloquent\Model;
use App\Http\Util\quota\aop\AopClient;
use App\Http\Util\quota\aop\request\ZhimaCreditScoreGetRequest;
use App\Http\Util\quota\aop\request\AlipayOpenAuthTokenAppRequest;
use App\Http\Util\quota\aop\request\AlipaySystemOauthTokenRequest;

use Illuminate\Support\Facades\DB;

class Quota extends Model
{
    protected $table='quota';
    //
    public function __construct()
    {
       //
    }

    /**额度申请信息表*/
    public static function quota_apply($data)
    {
        $res = DB::table('applyquota_message')->insertGetId($data);
        return $res;
    }

    /**
     * @transaction_id 商户请求唯一标识
    */
    public static function ident($transaction_id,$accessToken)
    {
        $json = "{" .
            "\"transaction_id\":\"$transaction_id\"," .
            "\"product_code\":\"w1010100100000000001\"" .
            "}";
        $res = self::zhima($json,$accessToken);
        return $res;

    }

    /**
     * 信用额度授权数据接收[使用app_auth_code换取app_auth_token]
     * @grant_type 授权类型
     * @code 授权码
     */
    public static function getzhima_auth_code($grant_type,$code=null)
    {
        $json="{" .
            "\"grant_type\":\"$grant_type\"," .
            "\"code\":\"$code\""." }";
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = trim('2017100909208205');
        $aop->rsaPrivateKey = trim('MIIEowIBAAKCAQEAmrCf2qoUMhaeLH9wuv28A1BEHphnhz0xn2i6JJUUvdnrBBcI9HEezXsIx+UuxAU23HItjvweh2BvqXwUQ9bOLQMc251Tk1HEQt1if/qdYVfHMbawBdf+dngCg1tpSWojsFnV6Mua83r09RZTsGtRE2IwboZSOTJuhrsWkSyxbGe/Ak6BEdogFjM7v7TV5eDXTTX/JGlLSK3R4xG6OSeCr1+nMKiphdBdQ97qc6DoqKyLs+eZFTrH41IHwPsQrVKxrvfDZxYNENgzsdobLDBzajluBJaqXJS6LC7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gTd3+A0L9G2jFbILtJJWq0AQKBgFvMaTj7qOXkG2Yf8lIh3WOqIo5g858pf8R06MN4nrY2zvnJQkdCM+EXHtTUvt2IPsDKA6XOZVcUQYnIue53UXwc411Gl6udayeiP5bURCrZ1KMeBVcOLrTLi7H39nZ4JA9J8k8eP261t9L7G7lPYO1jOLpNlSwPnGXz0umAdonM');
        $aop->alipayrsaPublicKey=trim('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo09Bai8ppFtNQayITHf+S7LnfofDEKPxLS1BsXemm+kgFK/O3zKcqDAdBdZOXgj336nJg/+K724LkBUs+NxumcAKv6dPXhRix5s1kyWsnP5U5LL0QtvgbxWl7trv+gnvhXH8TMwRk98zLN3TnHdv5eFs270rJ+lQoCK6nuScGR2jUlHrAuZoccufo8KLNgqyEvIl+nUzbLJVfw+AXDRqD47zR4j/e0S2CyWUeb3kCWBA2Msli1Ef1s9ZU8eDl1sQdn8TrPTtt1In+i//uXrDLEfg9kRpBoq5uBmo05PmFk2vCnXL4o9EmEwWHVMi3/mxYdAoTVqZCYlUmQlM68r/FQIDAQAB');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new AlipaySystemOauthTokenRequest();
        //$request->setBizContent($json);
        $request->setCode($code);
        $request->setGrantType($grant_type);
        //$request->setRefreshToken($code);
        $res = $aop->execute($request);
        return $res;
    }




    /**
     * 信用额度调取接口
     * @https://openapi.alipaydev.com/gateway.do 沙箱测试环境
     * @https://openapi.alipay.com/gateway.do 生产环境
     * @appid 2016080800191808 沙箱测试环境appid
     * @appid 2017100909208205 生产appid
     * @生产公钥  $aop->alipayrsaPublicKey=trim('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo09Bai8ppFtNQayITHf+S7LnfofDEKPxLS1BsXemm+kgFK/O3zKcqDAdBdZOXgj336nJg/+K724LkBUs+NxumcAKv6dPXhRix5s1kyWsnP5U5LL0QtvgbxWl7trv+gnvhXH8TMwRk98zLN3TnHdv5eFs270rJ+lQoCK6nuScGR2jUlHrAuZoccufo8KLNgqyEvIl+nUzbLJVfw+AXDRqD47zR4j/e0S2CyWUeb3kCWBA2Msli1Ef1s9ZU8eDl1sQdn8TrPTtt1In+i//uXrDLEfg9kRpBoq5uBmo05PmFk2vCnXL4o9EmEwWHVMi3/mxYdAoTVqZCYlUmQlM68r/FQIDAQAB');
     *
     * transaction_id
     * product_code[w1010100000000002733 ]
     * cert_type [IDENTITY_CARD身份证，ALIPAY_USER_ID支付宝id]
     * cert_no
     * name[证件是支付宝id时必填]
     * admittance_score[350～950之间 业务判断的准入标准,推荐650,芝麻信用分]
     */
    public static function zhima($json,$accessToken)
    {
       /**芝麻信用评分*/
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = trim('2017100909208205');
        $aop->rsaPrivateKey = trim('MIIEowIBAAKCAQEAmrCf2qoUMhaeLH9wuv28A1BEHphnhz0xn2i6JJUUvdnrBBcI9HEezXsIx+UuxAU23HItjvweh2BvqXwUQ9bOLQMc251Tk1HEQt1if/qdYVfHMbawBdf+dngCg1tpSWojsFnV6Mua83r09RZTsGtRE2IwboZSOTJuhrsWkSyxbGe/Ak6BEdogFjM7v7TV5eDXTTX/JGlLSK3R4xG6OSeCr1+nMKiphdBdQ97qc6DoqKyLs+eZFTrH41IHwPsQrVKxrvfDZxYNENgzsdobLDBzajluBJaqXJS6LC7m20edx7NGrQV9URiPAOL9ED4kfqxGEcfDMyamPHbt1e4Iq4TddQIDAQABAoIBAQCIDEVbRHBkqR3OFvtnihdcdrCik/mhqMFG6Q2ldPR8EPewbi3zBrZHlCjh3gAmaEua0FSfR7/14mvyjO0pyPcmAXHU+OZzT9ujzx99XUYjQdcwMvNXAGONf/j93FyaM95VkH0gtEmAUKi2ET/jjk8NFDNyUXJpWaB5oHu1jLAynSHRj8sazoQfnkg9BavDzDjEZMKPjn/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AQEAo09Bai8ppFtNQayITHf+S7LnfofDEKPxLS1BsXemm+kgFK/O3zKcqDAdBdZOXgj336nJg/+K724LkBUs+NxumcAKv6dPXhRix5s1kyWsnP5U5LL0QtvgbxWl7trv+gnvhXH8TMwRk98zLN3TnHdv5eFs270rJ+lQoCK6nuScGR2jUlHrAuZoccufo8KLNgqyEvIl+nUzbLJVfw+AXDRqD47zR4j/e0S2CyWUeb3kCWBA2Msli1Ef1s9ZU8eDl1sQdn8TrPTtt1In+i//uXrDLEfg9kRpBoq5uBmo05PmFk2vCnXL4o9EmEwWHVMi3/mxYdAoTVqZCYlUmQlM68r/FQIDAQAB');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new ZhimaCreditScoreGetRequest ();
        $request->setBizContent($json);
        $result = $aop->execute ( $request , $accessToken );
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

    /**
     * 调用异常处理接口
     * 接口异常时调用
     */
    public static function do_error()
    {
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = trim('2017100909208205');
        $aop->rsaPrivateKey = trim('MIIEowIBAAKCAQEAmrCf2qoUMhaeLH9wuv28A1BEHphnhz0xn2i6JJUUvdnrBBcI9HEezXsIx+UuxAU23HItjvweh2BvqXwUQ9bOLQMc251Tk1HEQt1if/qdYVfHMbawBdf+dngCg1tpSWojsFnV6Mua83r09RZTsGtRE2IwboZSOTJuhrsWkSyxbGe/Ak6BEdogFjM7v7TV5eDXTTX/JGlLSK3R4xG6OSeCr1+nMKiphdBdQ97qc6DoqKyLs+eZFTrH41IHwPsQrVKxrvfDZxYNENgzsdobLDBzajluBJaqXJS6LC7m20edx7NGrQV9URiPAOL9ED4kfqxGEcfDMyamPHbt1e4Iq4TddQIDAQABAoIBAQCIDEVbRHBkqR3OFvtnihdcdrCik/mhqMFG6Q2ldPR8EPewbi3zBrZ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411Gl6udayeiP5bURCrZ1KMeBVcOLrTLi7H39nZ4JA9J8k8eP261t9L7G7lPYO1jOLpNlSwPnGXz0umAdonM');
        $aop->alipayrsaPublicKey=trim('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo09Bai8ppFtNQayITHf+S7LnfofDEKPxLS1BsXemm+kgFK/O3zKcqDAdBdZOXgj336nJg/+K724LkBUs+NxumcAKv6dPXhRix5s1kyWsnP5U5LL0QtvgbxWl7trv+gnvhXH8TMwRk98zLN3TnHdv5eFs270rJ+lQoCK6nuScGR2jUlHrAuZoccufo8KLNgqyEvIl+nUzbLJVfw+AXDRqD47zR4j/e0S2CyWUeb3kCWBA2Msli1Ef1s9ZU8eDl1sQdn8TrPTtt1In+i//uXrDLEfg9kRpBoq5uBmo05PmFk2vCnXL4o9EmEwWHVMi3/mxYdAoTVqZCYlUmQlM68r/FQIDAQAB');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $query = new AlipayFundAuthOperationDetailQueryRequest();
        $cancel = new AlipayFundAuthOperationCancelRequest();
        $aop->execute ($query);
        $aop->execute ($cancel);

    }


    /**用户额度分审核记录入库*/
    public static function insertquota($data)
    {
        $res = DB::table('quota')->insertGetId($data);
        return $res;
    }

    /**
     * 额度使用详情
     * [商品主图，租金，家具名，使用额度，订单状态，删除改为隐藏操作]
     */
    public static function quota_data_dt($id)
    {
        $res = DB::table('quota_detail as qt_dt')
            ->leftJoin('quota as qt','qt.quota_id','=','qt_dt.quota_detail_quota_id')
            ->leftJoin('orders as od','od.id','=','qt_dt.quota_detail_order_id')
            ->leftJoin('order_details as od_dt','od_dt.details_order_id','=','od.id')
            ->leftJoin('fitment_details as ft_dt','ft_dt.details_id','=','od_dt.details_fitment_id')
            ->leftJoin('details_img as img','img.fitment_details_id','=','od_dt.details_fitment_id')
            ->leftJoin('fitments as ft','ft.fitments_id','=','ft_dt.details_pid')
            ->where('quota_detail_quota_id',$id)
            ->where('qt_dt.quota_detail_isdel',1)
            ->select('img.fitment_details_imgMaster','ft_dt.details_id','ft_dt.details_pid','ft_dt.details_name', 'ft_dt.details_length',
                'ft_dt.details_width','ft_dt.details_bond','ft_dt.details_nimbleprice','ft_dt.details_fixprice', 'ft.fitments_name',
                'od_dt.details_fitment_number','od_dt.details_rent_type','od.orders_status',
                'qt_dt.created_at','qt_dt.quota_detail_amount','qt_dt.quota_detail_status','qt_dt.quota_detail_status','qt_dt.quota_detail_id')
            ->get();
        return $res;
    }
}
